﻿using RabbitMQ.Client;
using System;

namespace GT.Framework.RabbitMQ.Core
{
    public class RabbitMQManager
    {
        /// <summary>
        /// RabbitMQ配置文件信息
        /// </summary>
        private static RabbitMQConfig RabbitMQConfig = RabbitMQConfig.GetConfig();

        private static IConnectionFactory factory;
        /// <summary>
        /// 静态构造方法，初始化链接池管理对象
        /// </summary>
        static RabbitMQManager()
        {
            CreateManager();
        }

        /// <summary>
        /// 创建链接池管理对象
        /// </summary>
        private static void CreateManager()
        {
            var uri = new Uri(RabbitMQConfig.ServerConStr);
            factory = new ConnectionFactory
            {
                UserName = RabbitMQConfig.UserName,
                Password = RabbitMQConfig.Password,
                RequestedHeartbeat = RabbitMQConfig.RequestedHeartbeat,
                Endpoint = new AmqpTcpEndpoint(uri)
            };
        }

        public static IConnection GetClient()
        {
            if (factory == null)
                CreateManager();
            return factory.CreateConnection();
        }
    }
}
